//
//  ChooseAddressController.m
//  PosApp
//
//  Created by 李英新 on 2018/11/7.
//  Copyright © 2018 李英新. All rights reserved.
//

#import "ChooseAddressController.h"

@interface ChooseAddressController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *ChooseV;
@property (strong, nonatomic) NSDictionary *pickerDic;
@property (strong, nonatomic) NSArray *selectedArray;
@property (strong, nonatomic) NSArray *provinceArray;
@property (strong, nonatomic) NSArray *cityArray;

@property (strong, nonatomic) NSString * ShengStr;
@property (strong, nonatomic) NSString * ShiStr;
@end

@implementation ChooseAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.ShengStr = @"湖南省";
    self.ShiStr = @"岳阳市";
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    self.pickerDic = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.provinceArray = [self.pickerDic allKeys];
    self.selectedArray = [self.pickerDic objectForKey:[[self.pickerDic allKeys] objectAtIndex:0]];
    if (self.selectedArray.count > 0) {
        self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
    }
    
    
    self.ChooseV.delegate   = self;
    self.ChooseV.dataSource = self;
    [self.ChooseV reloadAllComponents];
    [self updateAddress];
    
}
- (void)updateAddressAtProvince:(NSString *)province city:(NSString *)city {
    self.province = province;
    self.city = city;
    
    if (self.province) {
        for (NSInteger i = 0; i < self.provinceArray.count; i++) {
            NSString *city = self.provinceArray[i];
            NSInteger select = 0;
            if ([city isEqualToString:self.province]) {
                select = i;
                [self.ChooseV selectRow:i inComponent:0 animated:YES];
                break;
            }
        }
        self.cityArray = [self.pickerDic[self.province][0] allKeys];
        for (NSInteger i = 0; i < self.cityArray.count; i++) {
            NSString *city = self.cityArray[i];
            if ([city isEqualToString:self.city]) {
                [self.ChooseV selectRow:i inComponent:1 animated:YES];
                break;
            }
        }
        
    }
    [self.ChooseV reloadAllComponents];
    [self updateAddress];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.provinceArray.count;
    } else {
        return self.cityArray.count;
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return self.view.frame.size.width / 2;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:self.font?:[UIFont boldSystemFontOfSize:14]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}



- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return [self.provinceArray objectAtIndex:row];
    } else{
        return [self.cityArray objectAtIndex:row];
    }
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectedArray = [self.pickerDic objectForKey:[self.provinceArray objectAtIndex:row]];
        if (self.selectedArray.count > 0) {
            self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
        } else {
            self.cityArray = @[];
        }
        
        [pickerView reloadComponent:1];
        [pickerView selectedRowInComponent:1];
        
    }

    [self updateAddress];
    
    
    self.ShengStr = [NSString stringWithFormat:@"%@",[self.provinceArray objectAtIndex:[self.ChooseV selectedRowInComponent:0]]];
    self.ShiStr = [NSString stringWithFormat:@"%@",[self.cityArray objectAtIndex:[self.ChooseV selectedRowInComponent:1]]];
    
}


- (void)updateAddress {
    self.province = [self.provinceArray objectAtIndex:[self.ChooseV selectedRowInComponent:0]];
    self.city  = [self.cityArray objectAtIndex:[self.ChooseV selectedRowInComponent:1]];
    
}

- (IBAction)ClickOnBtnToUpAddress:(id)sender {
    if (self.ShiStr.length > 0) {
        [self.delegate ClickOnBtnToUploadChooseAddressShengString:self.ShengStr andShi:self.ShiStr];
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)ClickOnBtnToDismissChooseAddress:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}



@end
